package com.djzhou.gmms.dictionary.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.dictionary.TbDictionary;
import com.djzhou.gmms.model.dictionary.TbDictionaryType;
import com.djzhou.gmms.model.position.TbPosition;
import com.opensymphony.xwork2.ModelDriven;

public class DictionaryAction extends BaseAction<TbDictionary> implements ModelDriven<TbDictionary> {

	private TbDictionary dictionary;
	private TbDictionaryType dictionaryType;
	private Integer dictionaryId;
	private Integer dictionaryTypeId;
	private List<TbDictionary> list;
	private List<TbDictionaryType> dictionaryTypeList;
	
	
	@Override
	public String delete() {
		dictionaryService.delete(dictionaryId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("数据字典",queryUser(),"删除",dictionaryId,"TbDictionary",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		TbDictionaryType dictionaryType=dictionaryTypeService.queryById(dictionaryTypeId);
		dictionary.setDictionaryType(dictionaryType);
		dictionary.setLastModifyTime(new Date());
		dictionary.setUser(queryUser());
		dictionary.setDeleteFlag(0);
		dictionaryService.update(dictionary);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("数据字典",queryUser(),"编辑",dictionaryId,"TbDictionary",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		dictionaryTypeList=dictionaryTypeService.queryList(hashMap);
		if(dictionaryTypeId!=null && dictionaryTypeId!=0)hashMap.put("dictionaryTypeId", dictionaryTypeId);
		
		pageBean=dictionaryService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=dictionaryService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbDictionary>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		if(dictionary!=null){
			TbDictionaryType dictionaryType=dictionaryTypeService.queryById(dictionaryTypeId);
			dictionary.setDictionaryType(dictionaryType);
			Date date=new Date();
			dictionary.setCreateTime(date);
			dictionary.setLastModifyTime(date);
			dictionary.setDeleteFlag(0);
			dictionary.setUser(queryUser());
			dictionaryService.add(dictionary);
		}
		logService.addLog("数据字典",queryUser(),"添加",dictionaryId,"TbDictionary",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		dictionaryTypeList=dictionaryTypeService.queryList(null);
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		dictionaryTypeList=dictionaryTypeService.queryList(null);
		dictionary=dictionaryService.queryById(dictionaryId);
		return SUCCESS;
	}

	public TbDictionary getModel() {
		return dictionary;
	}

	public TbDictionary getDictionary() {
		return dictionary;
	}

	public void setDictionary(TbDictionary dictionary) {
		this.dictionary = dictionary;
	}

	public TbDictionaryType getDictionaryType() {
		return dictionaryType;
	}

	public void setDictionaryType(TbDictionaryType dictionaryType) {
		this.dictionaryType = dictionaryType;
	}

	public Integer getDictionaryId() {
		return dictionaryId;
	}

	public void setDictionaryId(Integer dictionaryId) {
		this.dictionaryId = dictionaryId;
	}

	public Integer getDictionaryTypeId() {
		return dictionaryTypeId;
	}

	public void setDictionaryTypeId(Integer dictionaryTypeId) {
		this.dictionaryTypeId = dictionaryTypeId;
	}

	public List<TbDictionary> getList() {
		return list;
	}

	public void setList(List<TbDictionary> list) {
		this.list = list;
	}

	public List<TbDictionaryType> getDictionaryTypeList() {
		return dictionaryTypeList;
	}

	public void setDictionaryTypeList(List<TbDictionaryType> dictionaryTypeList) {
		this.dictionaryTypeList = dictionaryTypeList;
	}
	

}
